package com.chuanshiyidao.huawei;

import android.app.Activity;
import android.net.Uri;

import com.huawei.hmf.tasks.OnFailureListener;
import com.huawei.hmf.tasks.OnSuccessListener;
import com.huawei.hmf.tasks.Task;
import com.huawei.hms.common.ApiException;
import com.huawei.hms.support.account.AccountAuthManager;
import com.huawei.hms.support.account.request.AccountAuthParams;
import com.huawei.hms.support.account.request.AccountAuthParamsHelper;
import com.huawei.hms.support.account.result.AuthAccount;
import com.huawei.hms.support.account.service.AccountAuthService;

import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class HuaweiLoginManager {
    private Activity activity;
    private LoginCallback loginCallback;
    public HuaweiLoginManager(Activity activity, LoginCallback loginCallback){
        this.activity= activity;
        this.loginCallback = loginCallback;
    }

    /**
     *
     * @param requestCode 8888:login 9999:pay
     */
    public void login(int requestCode){
        AccountAuthParams authParams = new AccountAuthParamsHelper(AccountAuthParams.DEFAULT_AUTH_REQUEST_PARAM).setAuthorizationCode().createParams();
        AccountAuthService service = AccountAuthManager.getService(activity, authParams);
        activity.startActivityForResult(service.getSignInIntent(), requestCode);
    }

    /**
     * 静默登录
     */
    public void loginSilence(){
        AccountAuthParams authParams = new AccountAuthParamsHelper(AccountAuthParams.DEFAULT_AUTH_REQUEST_PARAM)
                .createParams();
        AccountAuthService service = AccountAuthManager.getService(activity, authParams);
        Task<AuthAccount> task = service.silentSignIn();
        task.addOnSuccessListener(new OnSuccessListener<AuthAccount>() {
            @Override
            public void onSuccess(AuthAccount authAccount) {
                //获取帐号信息
//                Utils.log("displayName:" + authAccount.getDisplayName());
//                //获取帐号类型，0表示华为帐号、1表示AppTouch帐号
//                Utils.log( "accountFlag:" + authAccount.getAccountFlag());
                Utils.log("slience login code:" + getMD5(authAccount.getOpenId()));
                if(loginCallback != null){
                    loginCallback.silenceLoginSuccess(authAccount.getOpenId());
                }
            }
        });

        task.addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(Exception e) {
                //登录失败，您可以尝试使用getSignInIntent()方法显式登录
                if (e instanceof ApiException) {
                    ApiException apiException = (ApiException) e;
                    Utils.log("sign failed status:" + apiException.getStatusCode());
                    if(loginCallback != null){
                        loginCallback.silenceLoginFailed();
                    }
                }
            }
        });

    }

    private String getMD5(String msg){
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("MD5");
            md.update(msg.getBytes());
            byte[] digest = md.digest();
            String myHash = ByteUtils.toHexString(digest).toLowerCase();
            return myHash;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }

}

interface LoginCallback{
    void silenceLoginFailed();
    void silenceLoginSuccess(String authCode);
}
